package com.whiteape.book.model.dto;

import android.os.Parcel;
import android.os.Parcelable;

import com.whiteape.book.model.bo.CategoryDetailBo;
import com.whiteape.book.utils.MapConverter;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.ToMany;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.greenrobot.greendao.DaoException;
import com.whiteape.book.model.gen.DaoSession;
import com.whiteape.book.model.gen.CategoryDetailBoDao;
import com.whiteape.book.model.gen.SiteSourceCategoryBeanDao;

@Entity
public class SiteSourceCategoryBean implements Parcelable {
    //拿到分类名字+url
    public static final long serialVersionUID = 12333212314566L;
    @Index
    private String siteHostUrl;
    private String categoryName;
    @Id
    private String categoryUrl;

    @ToMany(referencedJoinProperty = "categoryUrl")
    private List<CategoryDetailBo> categoryDetailBoList;

    @Convert(columnType = String.class, converter = MapConverter.class)
    private HashMap<String, String> varMap;
    /** Used to resolve relations */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;
    /** Used for active entity operations. */
    @Generated(hash = 2080759553)
    private transient SiteSourceCategoryBeanDao myDao;

    public HashMap<String, String> getVarMap() {
        return varMap;
    }

    public void setVarMap(HashMap<String, String> varMap) {
        this.varMap = varMap;
    }

    public static Creator CREATOR = new Creator() {
        @Override
        public Object createFromParcel(Parcel source) {
            return new SiteSourceCategoryBean(source);
        }

        @Override
        public Object[] newArray(int size) {
            return new Object[0];
        }
    };

    @Generated(hash = 2001853264)
    public SiteSourceCategoryBean(String siteHostUrl, String categoryName,
            String categoryUrl, HashMap<String, String> varMap) {
        this.siteHostUrl = siteHostUrl;
        this.categoryName = categoryName;
        this.categoryUrl = categoryUrl;
        this.varMap = varMap;
    }

    @Generated(hash = 743923842)
    public SiteSourceCategoryBean() {
    }

    public String getSiteHostUrl() {
        return this.siteHostUrl;
    }
    public void setSiteHostUrl(String siteHostUrl) {
        this.siteHostUrl = siteHostUrl;
    }
    public String getCategoryName() {
        return this.categoryName;
    }
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
    public String getCategoryUrl() {
        return this.categoryUrl;
    }
    public void setCategoryUrl(String categoryUrl) {
        this.categoryUrl = categoryUrl;
    }

    public SiteSourceCategoryBean(Parcel in) {
        this.siteHostUrl = in.readString();
        this.categoryName = in.readString();
        this.categoryUrl = in.readString();
        this.varMap = in.readHashMap(HashMap.class.getClassLoader());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(siteHostUrl);
        dest.writeString(categoryName);
        dest.writeString(categoryUrl);
        dest.writeMap(varMap);

    }

    /**
     * To-many relationship, resolved on first access (and after reset).
     * Changes to to-many relations are not persisted, make changes to the target entity.
     */
    @Generated(hash = 1848538886)
    public List<CategoryDetailBo> getCategoryDetailBoList() {
        if (categoryDetailBoList == null) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            CategoryDetailBoDao targetDao = daoSession.getCategoryDetailBoDao();
            List<CategoryDetailBo> categoryDetailBoListNew = targetDao
                    ._querySiteSourceCategoryBean_CategoryDetailBoList(categoryUrl);
            synchronized (this) {
                if (categoryDetailBoList == null) {
                    categoryDetailBoList = categoryDetailBoListNew;
                }
            }
        }
        return categoryDetailBoList;
    }

    /** Resets a to-many relationship, making the next get call to query for a fresh result. */
    @Generated(hash = 454798741)
    public synchronized void resetCategoryDetailBoList() {
        categoryDetailBoList = null;
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 128553479)
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 1942392019)
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 713229351)
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 858890119)
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getSiteSourceCategoryBeanDao()
                : null;
    }

}
